package graphEd.saisie;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.mxgraph.util.mxResources;

public class Parcourir extends JPanel{
	private static final long serialVersionUID = 1L;
	private JButton button = new JButton(mxResources.get("browse"));
	private JTextField chemin = new JTextField(25);
	private JLabel label;
	@SuppressWarnings("unused")
	private String s;
	private int fileSelectionMode = JFileChooser.DIRECTORIES_ONLY;
	
	public String getPath(){
		return chemin.getText();
	}
	
	public void setFileSelectionMode(int fileSelectionMode){
		this.fileSelectionMode = fileSelectionMode;
	}
	
	public Parcourir(String s) {
		this.s = s;
		label = new JLabel(mxResources.get(s), JLabel.RIGHT);
		label.setLabelFor(chemin);// ?
		//label.setBorder(BorderFactory.createLineBorder(Color.blue));
		
		SpringLayout layout = new SpringLayout();
		this.setLayout(layout);
	
		layout.putConstraint(SpringLayout.WEST, label, 5,
				SpringLayout.WEST, this);

		layout.putConstraint(SpringLayout.EAST, chemin, 0,
				SpringLayout.WEST, button);
		layout.putConstraint(SpringLayout.WEST, chemin, 160,
				SpringLayout.WEST, this);

		layout.putConstraint(SpringLayout.EAST, button, -5,
				SpringLayout.EAST, this);
		
		layout.putConstraint(SpringLayout.EAST, label, -10,
				SpringLayout.WEST, chemin);
		 

		this.add(label);
		this.add(chemin );
		this.add(button);

		button.addActionListener(new ActionListener(){
		    public void actionPerformed(ActionEvent e){
		        JFileChooser chooser = new JFileChooser();
		        chooser.setFileSelectionMode(fileSelectionMode);
		        int returnVal = chooser.showOpenDialog(null);
		        if(returnVal == JFileChooser.APPROVE_OPTION) {
		           chemin.setText(chooser.getSelectedFile().getAbsolutePath());
		        }
		 
		    }
		});
	}
	
	public String getText(){
		if(chemin.getText()!=null)
			return chemin.getText();
		else
			return "";
	}
	
	public void setText(String path){
		chemin.setText(path);
	}
	
}